use std::fs::File;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::io::BufReader;
use std::{thread, time};
use std::time::Duration;
use rand::Rng;
use whosly::duration::DurationX;

// 多线程文件读写
fn main() {
    let fileName = String::from("/tmp/dst");

    // vector 只能储存相同类型的值
    let v = vec![String::from("sefghjhgfsdfghfdsa"),
                 String::from("张赛道好玩idhwdjodo"),
                 String::from("阿斯差u"),
                 String::from("啊是地方规划局o"),
                 String::from("苦于他热舞是"),
                 String::from("机会特务"),
                 String::from("请问人以后")];
    write_file_thread(&fileName, &v);

    let appendV = vec![String::from("寄到德金哦阿斯"),
                       String::from("个人待规划基数"),
                       String::from("AERKL,MNB"),
                       String::from("人体育课ij"),
                       String::from("玩儿他遇见你"),
                       String::from("玩儿体育局"),
                       String::from("哦iut"),
                       String::from("离开家华国锋")];
    write_append_file_thread(&fileName, &appendV);

    let ten_millis = time::Duration::from_millis(100);
    let now = time::Instant::now();
    thread::sleep(ten_millis);

    read_file_line_thread(&fileName);
}

fn write_file_thread(file: &String, bodys: &Vec<String>){
    println!("写入文件");

    let now = DurationX::new();
    let mut f = File::create(file).unwrap();

    // Rust 中通过 std::thread::spawn 函数创建新线程
    // https://blog.csdn.net/phthon1997/article/details/125897531
    // https://blog.csdn.net/weixin_52617269/article/details/125786204
    // https://www.bilibili.com/read/cv15175957

    for body in bodys {
        f.write(body.as_bytes()).unwrap();
        f.write("\n".as_bytes()).unwrap();
    }

    let duration = now.release();
    println!("耗时：{}", duration);
}

fn write_append_file_thread(file: &String, bodys: &Vec<String>){
    println!("追加文件文件");

    let now = DurationX::new();
    let mut f = OpenOptions::new().create(true).append(true)
        .open(file).unwrap();

    for body in bodys {
        f.write(body.as_bytes()).unwrap();
        f.write("\n".as_bytes()).unwrap();
    }
    let duration = now.release();
    println!("耗时：{}", duration);
}

fn read_file_line_thread(file: &String){
    println!("===========================================");
    println!("逐行读取");
    println!("===========================================");

    let now = DurationX::new();
    let f = File::open(file).unwrap();

    let reader = BufReader::new(f);
    for line in reader.lines() {
        // line 是 std::result::Result<std::string::String, std::io::Error> 类型
        // line 不包含换行符
        let line = line.unwrap();
        println!("line: {}", line);
    }
    let duration = now.release();
    println!("耗时：{}", duration);
}

// 生成随机unicode字符串
fn get_random_string(len: usize) -> String {
    rand::thread_rng()
        .sample_iter::<char, _>(rand::distributions::Standard)
        .take(len)
        .collect()
}

// 生成随机数字
fn get_random_num() -> i32 {
    rand::thread_rng().gen_range(1..=100)
}